<?php

defined('BASEPATH') OR exit('No direct script access allowed');
/**
实现公共文件模板引入
**/
if ( ! function_exists('loading'))
{

	function loading($db,$session,$load,$action,$function,$data)
	{
		 $roleid=$session->userdata("roleid");

		   if(empty($roleid)){
				 	header("Location:index.php/login/login_out");
			 }
		$menus=root_menu($db,$session);
		$data['menus']=$menus;
		$data['action']=$function;
		$load->view($action,$data);

	}
}

if ( ! function_exists('read_point_list'))//递归获取数据库分类
{
        function read_point_list($db,$id){

                $id=$id;
                $db=$db;
                $arr = array();
                $sql="select * from point_list where parent=$id";
                $result=$db->query($sql);
                $arr=array();
                if(!empty($result)){
                        foreach($result->result_array() as $rows)
                        {
                                   $rows['children'] = read_point_list($db, $rows['id']);
                                   $rows['text']=$rows['name'];
                                   // $rows['jareaname']=read_areaname($db,$rows['jarea']);
                                   // $rows['jcityname']=read_areaname($db,$rows['jcity']);
                                   // $rows['jcountyname']=read_areaname($db,$rows['jcounty']);
                               $arr[] = $rows;

                        }

                          return $arr;
                }
        }

}


if ( ! function_exists('success'))
{

  function success()
  {

     $tmp=array("success"=>true);
                    $str=json_encode($tmp);
                    echo $str;

  }
}


if ( ! function_exists('ajaxsuccess'))
{

  function ajaxsuccess()
  {

     $tmp=array("status"=>"success");
                    $str=json_encode($tmp);
                    echo $str;

  }
}


if ( ! function_exists('ajaxfail'))
{

  function ajaxfail()
  {

     $tmp=array("status"=>"fail");
                    $str=json_encode($tmp);
                    echo $str;

  }
}

if ( ! function_exists('load'))
{

  function load($db,$session,$load,$action,$function,$data)
  {

    $roleid=$session->userdata("roleid");
      if(empty($roleid))
      {
        redirect("index.php/login/login_out");
      }

    $data['action']=$function;

    $load->view($action,$data);

  }
}

if ( ! function_exists('message'))
{

	function message($url,$message)
	{
		echo $message;

	redirect($url);
	}
}


if( ! function_exists('root_menu')){//获取系统权限菜单

    function root_menu($db,$session){
      //$roleid=1;
    $roleid=$session->userdata("roleid");
     if(empty($roleid))
      {
        redirect("index.php/login/login_out");
      }
      $sql="select B.id,B.menu,B.action,B.method,B.type,B.parent,A.role_id from `function_to_role` as A,`function` as B where A.`function_id`=B.`id` and A.`role_id`='$roleid' and B.`type`=1 and B.`parent`=-1 order by B.id ASC";
       $query=$db->query($sql);
      if($query->num_rows()>0)
      {
        $arr=$query->result_array();
        foreach ($arr as $key => $value) {
                foreach ($value as $key2 => $value) {
                        if($key2=="id")
                                {
                                        $sql="select B.id,B.menu,B.action,B.method,B.type,B.parent,A.role_id from `function_to_role` as A,`function` as B where A.`function_id`=B.`id` and A.`role_id`='$roleid' and B.`type`=1 and B.`parent`='$value' order by B.id ASC";
                        $query=$db->query($sql);
                        if($query->num_rows()>0){
                                $childarr=$query->result_array();
                        }else{
                                $childarr=0;
                        }
                                }

                }
                $arr[$key]["childmenu"]=$childarr;
        }
      }
     return $arr;
    }
}



if ( ! function_exists('nav_text'))//递归获取文本导航
{
        function nav_text($db,$function_id,$str){

    $sql="select * from function where id='$function_id'";
                $query=$db->query($sql);
                if($query->num_rows()>0){
                $row=$query->row();
                $str=$str.nav_text($db,$row->parent,$str);
                $str=$str."<li><span>".$row->menu."</span></li>";
           }
 return $str;
        }

}

if ( ! function_exists('read_function'))//递归获取数据库分类
{
        function read_function($db,$id){
                $id=$id;
                $db=$db;
                global $str;
                $arr = array();

                $sql="select * from function where parent=$id";
                $result=$db->query($sql);
                $arr=array();
                if(!empty($result)){
                        foreach($result->result_array() as $rows)
                        {
                                   $rows['list'] = read_function($db, $rows['id']);
                                   $arr[] = $rows;
                        }
                          return $arr;
                }
        }

}


if ( ! function_exists('read_checked_function'))//获取勾选的菜单项
{
  function read_checked_function($db,$id,$roleid){
    $roleid=$roleid;
    $id=$id;
    $db=$db;
    $arr = array();

    $sql="select * from function where parent=$id";
    $result=$db->query($sql);
    $arr=array();
    if(!empty($result)){
      foreach($result->result_array() as $rows)
      {
           $rows['list'] = read_checked_function($db, $rows['id'],$roleid);
           $function_id=$rows['id'];

           $sql="select `id` from `function_to_role` where `function_id`='$function_id' and `role_id`='$roleid'";
             $query=$db->query($sql);
             $num=$query->num_rows();
             if($num){
                      $rows['checked']="true";
             }else{
                      $rows['checked']="false";
             }

             $arr[] = $rows;
      }
        return $arr;
    }
  }

}

if ( ! function_exists('read_areaname'))//递归获取数据库分类
{
        function read_areaname($db,$areaid){
 
            $sql="select area_name from area where area_id='$areaid'";
            //echo $sql;
            $query=$db->query($sql);
            $num=$query->num_rows();
            if($num>0){
            $row=$query->row();
            //print_r($row);
            return $row->area_name;
            }else{
              return "未设置";
            } 
        }

}
if ( ! function_exists('read_nav'))//递归获取数据库分类
{
        function read_nav($db,$id){

                $id=$id;
                $db=$db;
                $arr = array();
                $sql="select * from nav where parent=$id";
                $result=$db->query($sql);
                $arr=array();
                if(!empty($result)){
                        foreach($result->result_array() as $rows)
                        {
                                   $rows['children'] = read_nav($db, $rows['id']);
                                   $rows['text']=$rows['name'];
                                  
                               $arr[] = $rows;

                        }

                          return $arr;
                }
        }

}

if ( ! function_exists('read_depart'))//递归获取数据库分类
{
        function read_depart($db,$id){

                $id=$id;
                $db=$db;
                $arr = array();
                $sql="select * from depart where parent=$id";
                $result=$db->query($sql);
                $arr=array();
                if(!empty($result)){
                        foreach($result->result_array() as $rows)
                        {
                                   $rows['children'] = read_depart($db, $rows['id']);
                                   $rows['text']=$rows['name'];
                                   $rows['jareaname']=read_areaname($db,$rows['jarea']);
                                   $rows['jcityname']=read_areaname($db,$rows['jcity']);
                                   $rows['jcountyname']=read_areaname($db,$rows['jcounty']);
                               $arr[] = $rows;

                        }

                          return $arr;
                }
        }

}


if ( ! function_exists('read_category'))//递归获取数据库分类
{
        function read_category($db,$id){

                $id=$id;
                $db=$db;
                $arr = array();
                $sql="select * from empcate where parent=$id";
                $result=$db->query($sql);
                $arr=array();
                if(!empty($result)){
                        foreach($result->result_array() as $rows)
                        {
                                   $rows['children'] = read_category($db, $rows['id']);
                                   $rows['text']=$rows['name'];
                                  
                               $arr[] = $rows;

                        }

                          return $arr;
                }
        }

}



if ( ! function_exists('read_product_category'))//递归获取数据库分类
{
        function read_product_category($db,$id){

                $id=$id;
                $db=$db;
                $arr = array();
                $sql="select * from productcategory where parent=$id";
                $result=$db->query($sql);
                $arr=array();
                if(!empty($result)){
                        foreach($result->result_array() as $rows)
                        {
                                   $rows['children'] = read_product_category($db, $rows['id']);
                                   $rows['text']=$rows['name'];
                               $arr[] = $rows;

                        }

                          return $arr;
                }
        }

}



if ( ! function_exists('read_area_name'))//递归获取数据库分类
{
        function read_area_name($db,$id){

              $sql="select area_name from area where area_id=$id";
							$query=$db->query($sql);
							$row=$query->row();
							return $row->area_name;
        }

}



if ( ! function_exists('read_child_depart'))//递归获取数据库分类
{
	function read_child_depart($db,$id,$str){

		$id=$id;
		$db=$db;
		$arr = array();
		$sql="select id from depart where parent=$id ";
		$result=$db->query($sql);

		if(!empty($result)){
			foreach($result->result_array() as $rows)
			{
				   $str = read_child_depart($db, $rows['id'],$str);
			        $str=$str.",".$rows['id'];
			}
			  return $str.",".$id;
		}
	}

}



/*递归获取网点机构号*/
if ( ! function_exists('depart_tree'))//递归获取机构分类
{
       function depart_tree($db,$id=-1,&$result=array()){
        $sql="select id from depart where parent=$id";
        $query=$db->query($sql);
        $arr=$query->result_array();
        $num=count($arr);
        if($num>0){
        foreach ($arr as $key => $value) {
           $result[]=$value['id'];
           depart_tree($db,$value['id'],$result);
        }
        }
        return $result;

        }

}

if ( ! function_exists('depart_tree_result'))//递归获取机构分类
{
       function depart_tree_result($db,$id=-1){
            $arr=depart_tree($db,$id);
            $arr[]=$id;
            $str="";
            foreach ($arr as $key => $value) {
              $str=$str.",".$value;
            }
            return substr($str,1);
            
        }

}
/*递归获取网点机构号*/


?>
